package com.bgq365.study.springboot2.boot02web.controller;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ParamController {

    @GetMapping("/car/{id}/owner/{username}")
    public Map<String, Object> getCar(@PathVariable Integer id,
                                      @PathVariable String username,
                                      @PathVariable Map<String, String> pv,
                                      @RequestHeader("User-Agent") String userAgent,
                                      @RequestHeader Map<String, String> headers,
                                      @RequestParam Integer age,
                                      @RequestParam String[] inters,
                                      @RequestParam Map<String, Object> params,
                                      @CookieValue String _ga,
                                      @CookieValue("_ga") Cookie cookie) {
        HashMap<String, Object> hashMap = new HashMap<>();
//        hashMap.put("id", id);
//        hashMap.put("username", username);
//        hashMap.put("pv", pv);
//        hashMap.put("userAgent", userAgent);
//        hashMap.put("headers", headers);
//        hashMap.put("age", age);
//        hashMap.put("inters", inters);
//        hashMap.put("params", params);
        hashMap.put("_ga", _ga);
        hashMap.put("cookie", cookie);
        return hashMap;
    }

    @PostMapping("/save")
    public Map<String, Object> postMethod(@RequestBody String content) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("content", content);
        return map;
    }

    /**
     * 1、语法：/cars/sell;low=21;brand=宝马,奔驰
     * 2、SpringBoot默认是禁用矩阵变量的功能
     * 手动开启：原理：WebMvcAutoConfiguration-->
     *                  configurePathMatch-->
     *                      UrlPathHelper-->
     *                          removeSemicolonContent = true (移除分号内容)
     */
    @GetMapping("/cars/{path}")
    public Map<String, Object> carsSell(@MatrixVariable Integer low,
                                        @MatrixVariable List<String> brand,
                                        @PathVariable String path) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("low", low);
        map.put("brand", brand);
        map.put("path", path);
        return map;
    }

    @GetMapping("/boss/{bossId}/{empId}")
    public Map<String, Object> boss(@MatrixVariable(value = "age", pathVar = "bossId") Integer bossAge,
                                    @MatrixVariable(value = "age", pathVar = "empId") Integer empAge,
                                    @PathVariable Integer bossId,
                                    @PathVariable Integer empId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("bossAge", bossAge);
        map.put("empAge", empAge);
        map.put("bossId", bossId);
        map.put("empId", empId);
        return map;
    }
}
